package com.doordu.soa.service.data.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.doordu.soa.service.comm.ResultCode;
import com.doordu.soa.service.data.service.ParkingCompanyService;
import com.doordu.soa.service.data.vo.ParkingCompanyVo;
import com.doordu.soa.service.web.annotation.NotLogin;
import com.doordu.soa.service.web.annotation.Ver;
import com.doordu.soa.service.web.auth.model.TokenModel;
import com.doordu.soa.service.web.utils.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * oauth接口授权
 * 
 * @ClassName: OauthController
 * @Description:
 * @author xiaoqun.yi yixq@doordu.com
 * @date 2017年4月6日 下午2:36:26
 * @version V1.0
 */
@Api(tags = "OauthController", description = "Token令牌接口")
@RestController
@RequestMapping("/parking/auth")
public class OauthController extends BaseController {
	protected static final Logger logger = LoggerFactory.getLogger(OauthController.class);

	@Autowired
	private ParkingCompanyService parkingCompanyService;

	@ApiOperation(value = "oauth接口授权生成token", notes = "oauth接口授权生成token")
	@ApiResponses({ @ApiResponse(code = 200, message = "处理成功") })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ver", value = "接口版本", paramType = "path", required = true, dataType = "String", defaultValue = "v1"),
			@ApiImplicitParam(name = "app_id", value = "应用id", paramType = "query", required = true, dataType = "String", defaultValue = "doorduadmin"),
			@ApiImplicitParam(name = "secret_key", value = "应用密钥", paramType = "query", required = true, dataType = "String", defaultValue = "doordadmin20170405") })
	@RequestMapping(path = "/getToken/{ver}", method = { RequestMethod.GET, RequestMethod.POST })
	@Ver("1")
	@NotLogin
	public ResponseEntity<Object> createToken(String app_id, String secret_key, HttpServletRequest request) {
		if (StringUtils.isBlank(app_id)) {
			return checkParamNull("appId应用id不能为空");
		}
		if (StringUtils.isBlank(secret_key)) {
			return checkParamNull("appSecret应用密钥不能为空");
		}
		ParkingCompanyVo vo = parkingCompanyService.selectByAppIdAndAppSecret(app_id, secret_key);
		if (null == vo) {
			return checkParamError("appId或appSecret不正确");
		}
		if (null == vo.getCompanyStatus() || vo.getCompanyStatus().intValue() == 0) {
			return error(ResultCode.ERROR_NOAUTH, "该停车场公司状态不合法");
		}
		TokenModel token = tokenManager.createToken(app_id, secret_key, vo.getCompanyId());
		return ok(token, "操作成功");
	}

}
